package com.bdqn.zhang.service.impl;

import com.bdqn.zhang.dao.UserMapper;
import com.bdqn.zhang.pojo.User;
import com.bdqn.zhang.service.UserService;
import com.bdqn.zhang.utils.Convert;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;
import java.util.List;

public class UserServiceImpl implements UserService {

    /*方式一*/
    /*@Override
    public int count() {
        UserDao userDao = new UserDaoImpl();
        return userDao.count();
    }*/

    @Override
    public int count() {
        String resource = "mybatis-config.xml";
        int count = 0;
        SqlSession sqlSession = null;
        try{
            //1 获取mybatis-config.xml的输入流
            InputStream is = Resources.getResourceAsStream(resource);
            //2 创建SqlSessionFactory对象，完成对配置文件的读取
            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
            //3 创建sqlSession
            sqlSession = factory.openSession();
            //4 调用mapper文件来对数据进行操作，必须先把mapper文件引入到mybatis-config.xml中
            count = sqlSession.selectOne("com.bdqn.zhang.dao.UserMapper.count");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return count;
    }

    @Override
    public List<User> findAll() {
        String resource = "mybatis-config.xml";
        List<User> userList = null;
        SqlSession sqlSession = null;
        try{
            //1 获取mybatis-config.xml的输入流
            InputStream is = Resources.getResourceAsStream(resource);
            //2 创建SqlSessionFactory对象，完成对配置文件的读取
            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
            //3 创建sqlSession
            sqlSession = factory.openSession();
            //4 调用mapper文件来对数据进行操作，必须先把mapper文件引入到mybatis-config.xml中
            userList = sqlSession.getMapper(UserMapper.class).findAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return userList;
    }

    @Override
    public User findUserById(Integer id) {
        String resource = "mybatis-config.xml";
        User user = null;
        SqlSession sqlSession = null;
        try{
            //1 获取mybatis-config.xml的输入流
            InputStream is = Resources.getResourceAsStream(resource);
            //2 创建SqlSessionFactory对象，完成对配置文件的读取
            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
            //3 创建sqlSession
            sqlSession = factory.openSession();
            //4 调用mapper文件来对数据进行操作，必须先把mapper文件引入到mybatis-config.xml中
            user = sqlSession.getMapper(UserMapper.class).findUserById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return user;
    }

    @Override
    public List<User> findAllByCondition(User user) {
        return null;
    }

    @Override
    public List<User> findAllByConditionAndPage(Integer address, Integer from, Integer pageSize) {
        return null;
    }

    @Override
    public int addUser(User user) {
        String resource = "mybatis-config.xml";
        int count = 0;
        SqlSession sqlSession = null;
        try{
            //1 获取mybatis-config.xml的输入流
            InputStream is = Resources.getResourceAsStream(resource);
            //2 创建SqlSessionFactory对象，完成对配置文件的读取
            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
            //3 创建sqlSession
            sqlSession = factory.openSession(false);
            //4 调用mapper文件来对数据进行操作，必须先把mapper文件引入到mybatis-config.xml中
            count = sqlSession.getMapper(UserMapper.class).addUser(user);
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return count;
    }

    @Override
    public int delUser(Integer id) {
        String resource = "mybatis-config.xml";
        int count = 0;
        SqlSession sqlSession = null;
        try{
            //1 获取mybatis-config.xml的输入流
            InputStream is = Resources.getResourceAsStream(resource);
            //2 创建SqlSessionFactory对象，完成对配置文件的读取
            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
            //3 创建sqlSession
            sqlSession = factory.openSession(false);
            //4 调用mapper文件来对数据进行操作，必须先把mapper文件引入到mybatis-config.xml中
            count = sqlSession.getMapper(UserMapper.class).delUser(id);
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return count;
    }

    @Override
    public int removeUsers(String ids) {
        String resource = "mybatis-config.xml";
        int count = 0;
        SqlSession sqlSession = null;
        try{
            //1 获取mybatis-config.xml的输入流
            InputStream is = Resources.getResourceAsStream(resource);
            //2 创建SqlSessionFactory对象，完成对配置文件的读取
            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
            //3 创建sqlSession
            sqlSession = factory.openSession(false);
            //4 调用mapper文件来对数据进行操作，必须先把mapper文件引入到mybatis-config.xml中
            count = sqlSession.getMapper(UserMapper.class).removeUsers(Convert.toStrArray(ids));
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return count;
    }

    @Override
    public int updateUser(User user) {
        String resource = "mybatis-config.xml";
        int count = 0;
        SqlSession sqlSession = null;
        try{
            //1 获取mybatis-config.xml的输入流
            InputStream is = Resources.getResourceAsStream(resource);
            //2 创建SqlSessionFactory对象，完成对配置文件的读取
            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
            //3 创建sqlSession
            sqlSession = factory.openSession(false);
            //4 调用mapper文件来对数据进行操作，必须先把mapper文件引入到mybatis-config.xml中
            count = sqlSession.getMapper(UserMapper.class).updateUser(user);
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return count;
    }
}
